{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "weighted-colombia",
   "metadata": {},
   "outputs": [],
   "source": [
    "import random\n",
    "import time\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "surprising-spring",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = []\n",
    "for i in range(100000000):\n",
    "    a.append(random.random())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "domestic-reader",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.4529998302459717"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t1 = time.time()\n",
    "sum1 = sum(a)\n",
    "t2 = time.time()\n",
    "t2 - t1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "heated-hands",
   "metadata": {},
   "outputs": [],
   "source": [
    "b = np.array(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "thrown-uganda",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.13000011444091797"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t4 = time.time()\n",
    "sum3 = np.sum(b)\n",
    "t5 = time.time()\n",
    "t5 - t4"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "israeli-mayor",
   "metadata": {},
   "source": [
    "### 常用属性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "incident-bidding",
   "metadata": {},
   "outputs": [],
   "source": [
    "import random\n",
    "import time\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "separated-monster",
   "metadata": {},
   "outputs": [],
   "source": [
    "data = np.array([1, 2, 3])  # 创建一个ndarray对象\n",
    "#  shift+tab键 查看帮助文档"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "directed-jenny",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "accredited-consultancy",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3,)"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.shape    # 查看数组的形状"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "governing-object",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.ndim   # 查看数维度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "shared-mileage",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.size    # 查看元素个数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "architectural-permission",
   "metadata": {},
   "outputs": [],
   "source": [
    "data1 = np.array([[1, 2, 3],\n",
    "                  [4, 5, 6]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "progressive-bracket",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [4, 5, 6]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "tight-eugene",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2, 3)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data1.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "acoustic-visit",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data1.ndim"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "together-flooring",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data1.size  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "destroyed-center",
   "metadata": {},
   "outputs": [],
   "source": [
    "data2 = np.array([\n",
    "                     [\n",
    "                         [1, 2, 3],\n",
    "                         [4, 5, 6]\n",
    "                     ],\n",
    "                    \n",
    "                    [\n",
    "                        [1, 2, 3],\n",
    "                        [4, 5, 6]\n",
    "                    ],\n",
    "    \n",
    "                    [\n",
    "                        [1, 2, 3],\n",
    "                        [4, 5, 6]\n",
    "                    ]\n",
    "                  ]\n",
    "                )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "formal-manual",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3, 2, 3)"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data2.shape   # 3, 2, 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "fantastic-detective",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data2.ndim"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "annoying-pepper",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "18"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data2.size"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "encouraging-annotation",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1.4, 2.5, 3.6])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "arr1 = np.array([1.4, 2.5, 3.6])\n",
    "arr1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "adjustable-preservation",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('float64')"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1.dtype   # 查看数据类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "fallen-depression",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr2 = np.array([1.4, 2.5, 3.6], dtype=np.int32)    # shift + tab  查看函数的帮助文档\n",
    "arr2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "blank-cotton",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1., 2., 3.], dtype=float32)"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr3 = np.array([1, 2, 3], dtype=np.float32)    # shift + tab  查看函数的帮助文档\n",
    "arr3         # 编辑多行，按ctrl选中"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "appointed-congress",
   "metadata": {},
   "source": [
    "### 创建数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "processed-climb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[6.23e-322, 0.00e+000, 0.00e+000],\n",
       "       [0.00e+000, 0.00e+000, 0.00e+000]])"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.empty([2,3])   #  shape参数可以使  元祖  列表  int型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "distant-agency",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 0, 0, 0, 0],\n",
       "       [1, 0, 0, 0, 0],\n",
       "       [0, 1, 0, 0, 0],\n",
       "       [0, 0, 1, 0, 0]])"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.eye(4,5, k=-1, dtype=np.int)     # 对角线矩阵, k是对角线的偏移量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "legitimate-radio",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 0., 0., 0.],\n",
       "       [0., 1., 0., 0.],\n",
       "       [0., 0., 1., 0.],\n",
       "       [0., 0., 0., 1.]])"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.identity(4)     # 单位矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "friendly-spell",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 1., 1., 1.],\n",
       "       [1., 1., 1., 1.],\n",
       "       [1., 1., 1., 1.]])"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.ones((3,4))       # 全一的矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "proprietary-seller",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0., 0., 0., 0., 0.],\n",
       "       [0., 0., 0., 0., 0.],\n",
       "       [0., 0., 0., 0., 0.],\n",
       "       [0., 0., 0., 0., 0.]])"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.zeros((4,5))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "promising-shelter",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3])"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "studied-highlight",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 1, 1])"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.ones_like(arr2)    # 生成一个 和指定数组类型以及形状保持一致的，由1填充的矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "portuguese-amount",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[9, 9, 9, 9],\n",
       "       [9, 9, 9, 9],\n",
       "       [9, 9, 9, 9]])"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.full((3, 4), 9)  # 填充为指定元素"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "amazing-uzbekistan",
   "metadata": {},
   "source": [
    "### 关于array和asarray的不同"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "technical-logan",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 1., 1., 1.],\n",
       "       [1., 1., 1., 1.],\n",
       "       [1., 1., 1., 1.]])"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr3 = np.ones((3,4))\n",
    "arr3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "overhead-workplace",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 1., 1., 1.],\n",
       "       [1., 1., 1., 1.],\n",
       "       [1., 1., 1., 1.]])"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data1 = np.array(arr3)    # 创建一个新的数组\n",
    "data1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "hazardous-boost",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 1., 1., 1.],\n",
       "       [1., 1., 1., 1.],\n",
       "       [1., 1., 1., 1.]])"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data2 = np.asarray(arr3)    # 创建一个数组的引用\n",
    "data2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "discrete-treaty",
   "metadata": {},
   "outputs": [],
   "source": [
    "arr3[1] = 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "genetic-island",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 1., 1., 1.],\n",
       "       [2., 2., 2., 2.],\n",
       "       [1., 1., 1., 1.]])"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "baking-maldives",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 1., 1., 1.],\n",
       "       [1., 1., 1., 1.],\n",
       "       [1., 1., 1., 1.]])"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "infrared-hebrew",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 1., 1., 1.],\n",
       "       [2., 2., 2., 2.],\n",
       "       [1., 1., 1., 1.]])"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "abstract-notice",
   "metadata": {},
   "source": [
    "### 创建固定范围的数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "colonial-france",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0., 2., 4., 6., 8.])"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.linspace(0, 10, 5, endpoint=False)   # 等比例划分数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "ethical-struggle",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([ 0.,  1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10.]), 1.0)"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.linspace(0, 10, 11, retstep=True)     # retstep返回数据，并将间隔也返回"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "german-niger",
   "metadata": {},
   "outputs": [],
   "source": [
    "data, step = np.linspace(0, 10, 5, retstep=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "black-museum",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0. ,  2.5,  5. ,  7.5, 10. ])"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "frank-healing",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2.5"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "step"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "mineral-visit",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 2, 4, 6, 8])"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.arange(0,10,2)       # 生成整型数列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "animal-integrity",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([   1.,   10.,  100., 1000.])"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.logspace(0, 3, 4)    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "id": "editorial-tourism",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(0.0, 1.0, 2.0, 3.0)"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import math\n",
    "math.log10(1), math.log10(10), math.log10(100),math.log10(1000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "id": "million-triple",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0., 1., 2., 3.])"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.linspace(0, 3,4)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "legendary-syndication",
   "metadata": {},
   "source": [
    "### 随机数生成（均匀分布）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "effective-tunnel",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "interested-intervention",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.70303634, 0.23337235, 0.7105717 ],\n",
       "       [0.21893777, 0.51985396, 0.42704803]])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.rand(2, 3)    # 生成指定维度 0-1之间的随机数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 193,
   "id": "north-conflict",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[19.73235064, 11.63358478, 18.25848119],\n",
       "       [16.14355223, 17.61941518, 15.25352935]])"
      ]
     },
     "execution_count": 193,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.uniform(10,20,size=(2,3))   # 生成指定范围的随机数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 194,
   "id": "delayed-handle",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[15, 13, 15, 17],\n",
       "       [18, 11, 13, 10],\n",
       "       [13, 12, 18, 13]])"
      ]
     },
     "execution_count": 194,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.randint(10, 20, size=(3,4))   # 生成指定范围和size的随机数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "electoral-african",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-7.36251629e-01,  2.06233324e+00,  3.78697022e-01, ...,\n",
       "        -1.18624586e-02,  7.05544426e+00, -3.15210202e+00],\n",
       "       [ 6.10043178e+00,  5.21940090e+00,  1.25763604e-02, ...,\n",
       "         1.75775211e+00, -1.35336345e+00,  9.30908178e+00],\n",
       "       [ 5.42487421e+00, -1.70031051e-02, -1.18146514e-01, ...,\n",
       "         6.24078840e+00,  1.99798574e+00, -5.71819337e+00],\n",
       "       ...,\n",
       "       [-3.56166441e+00,  5.07344819e+00,  1.11984143e+01, ...,\n",
       "         6.76163223e+00, -6.22932061e+00, -3.69952325e-01],\n",
       "       [ 7.06825253e+00,  8.17467121e-01, -3.28665302e+00, ...,\n",
       "         1.65165169e+00, -2.09221458e+00,  8.40744941e+00],\n",
       "       [ 1.46028731e+01,  3.41240406e+00, -1.68054177e+00, ...,\n",
       "        -4.04292437e+00, -5.50926170e+00,  7.34278976e+00]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stock_day_rise = np.random.normal(0, 5, (500, 504))\n",
    "stock_day_rise"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "banner-standing",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(500, 504)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stock_day_rise.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "comic-weight",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[192.08071422, 160.62803933, 173.95466969, ..., 171.72698193,\n",
       "        173.27502852, 172.51034789],\n",
       "       [181.05548519, 169.56160322, 157.01230524, ..., 153.40890962,\n",
       "        179.41253074, 165.18174634],\n",
       "       [173.04747284, 176.560167  , 153.33424938, ..., 166.49488879,\n",
       "        166.92392396, 184.22320658],\n",
       "       ...,\n",
       "       [160.18663042, 187.00181001, 156.40417497, ..., 174.79687657,\n",
       "        152.08085593, 162.42181881],\n",
       "       [173.30322667, 169.31648137, 159.5134958 , ..., 170.49660342,\n",
       "        171.07620672, 159.63720763],\n",
       "       [174.01997201, 192.30730182, 162.9494448 , ..., 165.66197812,\n",
       "        163.85532475, 175.18751355]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "high = np.random.normal(170, 10, (34,100))   # 模拟34个省份的身高数据\n",
    "high"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cellular-motel",
   "metadata": {},
   "source": [
    "### 数组的索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 208,
   "id": "opponent-assignment",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([  0.95425726,  -1.08428494,   1.72544504,  -1.69682584,\n",
       "         4.58561862,  -0.32719764,   0.50509453,   2.19775488,\n",
       "        -5.69115558,   3.87205121,   0.50883113,   3.05875488,\n",
       "        -1.27762855,  -0.0878556 ,  -8.97119032,   0.69291375,\n",
       "         4.36382704,   4.61180294,  -0.07270544,  -5.07495258,\n",
       "         1.33876058,   4.49654891,   0.04515872,  -3.8275143 ,\n",
       "        -1.80140077,  -5.1697216 ,   4.66577464,  -9.2824135 ,\n",
       "        -0.59545458,   2.09616976,   0.75271205,  -6.37150366,\n",
       "        -2.62603209,   6.89810182,  -0.44233876,  -8.72323046,\n",
       "         5.10291612,   1.19476485,   6.75109303,  -3.70953621,\n",
       "         3.5673111 ,   4.87241511,  -5.16463835,   6.55699558,\n",
       "        12.80068517,  -3.82734122,  -3.91302756,  -7.86631983,\n",
       "        -4.98553116,   4.08552308,   0.4031471 ,  -1.94337033,\n",
       "         0.30936382,   3.37538583,   0.36663009,   4.77099064,\n",
       "         7.3264609 ,   1.74728894,   8.76918818,   4.04544967,\n",
       "        -5.39311254,   0.37472536,   6.5281851 ,   6.29333964,\n",
       "         2.60033561,   5.37846556,   4.04605308,  16.58521616,\n",
       "        -0.33729714,  -0.94477587,  -1.1816341 ,  -8.37804153,\n",
       "        -2.80380631,   3.44141355,  14.38660457,   5.23645462,\n",
       "         0.74782484,   4.60818396,  -0.76898986, -10.91539931,\n",
       "         1.42914716,   2.70674597,   0.74783036,  -2.33186247,\n",
       "        -7.31872551,   7.39115795,   7.55092609,   0.54666864,\n",
       "         2.54517937,  -2.75975492,  -9.06616626,   3.87707622,\n",
       "        -3.66134912,   2.90396861,  -6.10926609,   0.45757836,\n",
       "         4.05079644,   3.03433208,  -2.31133316,   0.88781618])"
      ]
     },
     "execution_count": 208,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取第一个股票的前100个交易日的涨跌幅数据\n",
    "stock_day_rise[0, 0:100]   # numpy切片的写法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 209,
   "id": "criminal-diesel",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([  0.95425726,  -1.08428494,   1.72544504,  -1.69682584,\n",
       "         4.58561862,  -0.32719764,   0.50509453,   2.19775488,\n",
       "        -5.69115558,   3.87205121,   0.50883113,   3.05875488,\n",
       "        -1.27762855,  -0.0878556 ,  -8.97119032,   0.69291375,\n",
       "         4.36382704,   4.61180294,  -0.07270544,  -5.07495258,\n",
       "         1.33876058,   4.49654891,   0.04515872,  -3.8275143 ,\n",
       "        -1.80140077,  -5.1697216 ,   4.66577464,  -9.2824135 ,\n",
       "        -0.59545458,   2.09616976,   0.75271205,  -6.37150366,\n",
       "        -2.62603209,   6.89810182,  -0.44233876,  -8.72323046,\n",
       "         5.10291612,   1.19476485,   6.75109303,  -3.70953621,\n",
       "         3.5673111 ,   4.87241511,  -5.16463835,   6.55699558,\n",
       "        12.80068517,  -3.82734122,  -3.91302756,  -7.86631983,\n",
       "        -4.98553116,   4.08552308,   0.4031471 ,  -1.94337033,\n",
       "         0.30936382,   3.37538583,   0.36663009,   4.77099064,\n",
       "         7.3264609 ,   1.74728894,   8.76918818,   4.04544967,\n",
       "        -5.39311254,   0.37472536,   6.5281851 ,   6.29333964,\n",
       "         2.60033561,   5.37846556,   4.04605308,  16.58521616,\n",
       "        -0.33729714,  -0.94477587,  -1.1816341 ,  -8.37804153,\n",
       "        -2.80380631,   3.44141355,  14.38660457,   5.23645462,\n",
       "         0.74782484,   4.60818396,  -0.76898986, -10.91539931,\n",
       "         1.42914716,   2.70674597,   0.74783036,  -2.33186247,\n",
       "        -7.31872551,   7.39115795,   7.55092609,   0.54666864,\n",
       "         2.54517937,  -2.75975492,  -9.06616626,   3.87707622,\n",
       "        -3.66134912,   2.90396861,  -6.10926609,   0.45757836,\n",
       "         4.05079644,   3.03433208,  -2.31133316,   0.88781618])"
      ]
     },
     "execution_count": 209,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stock_day_rise[0][0:100]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 212,
   "id": "monthly-contamination",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2],\n",
       "       [3, 4],\n",
       "       [5, 6]])"
      ]
     },
     "execution_count": 212,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = np.arange(1,7).reshape(3,2)\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 213,
   "id": "indian-graham",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 213,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data[0, 1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 218,
   "id": "combined-african",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[3],\n",
       "       [5]])"
      ]
     },
     "execution_count": 218,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data[1:3, 0:1]        # 先行，再列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 219,
   "id": "ruled-vaccine",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2])"
      ]
     },
     "execution_count": 219,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data[0]      # 取第一个维度的第0个数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 220,
   "id": "marked-feeding",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2]])"
      ]
     },
     "execution_count": 220,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data[0:1]    # 切片，相当于在源数据上挖出一块   不改变维数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 221,
   "id": "respiratory-spanking",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2],\n",
       "       [3, 4]])"
      ]
     },
     "execution_count": 221,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data[0:2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 222,
   "id": "adopted-donna",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 3])"
      ]
     },
     "execution_count": 222,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data[0:2, 0]   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 223,
   "id": "final-enemy",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1],\n",
       "       [3]])"
      ]
     },
     "execution_count": 223,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data[0:2, 0:1]   "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "continuous-teaching",
   "metadata": {},
   "source": [
    "### 花式切片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 224,
   "id": "religious-significance",
   "metadata": {},
   "outputs": [],
   "source": [
    "arr4 = np.empty((10,10), dtype=np.int)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 225,
   "id": "certified-swift",
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in range(10):\n",
    "    arr4[i] = i"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 226,
   "id": "consistent-context",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n",
       "       [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],\n",
       "       [2, 2, 2, 2, 2, 2, 2, 2, 2, 2],\n",
       "       [3, 3, 3, 3, 3, 3, 3, 3, 3, 3],\n",
       "       [4, 4, 4, 4, 4, 4, 4, 4, 4, 4],\n",
       "       [5, 5, 5, 5, 5, 5, 5, 5, 5, 5],\n",
       "       [6, 6, 6, 6, 6, 6, 6, 6, 6, 6],\n",
       "       [7, 7, 7, 7, 7, 7, 7, 7, 7, 7],\n",
       "       [8, 8, 8, 8, 8, 8, 8, 8, 8, 8],\n",
       "       [9, 9, 9, 9, 9, 9, 9, 9, 9, 9]])"
      ]
     },
     "execution_count": 226,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 229,
   "id": "hydraulic-feeling",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n",
       "       [3, 3, 3, 3, 3, 3, 3, 3, 3, 3],\n",
       "       [5, 5, 5, 5, 5, 5, 5, 5, 5, 5],\n",
       "       [6, 6, 6, 6, 6, 6, 6, 6, 6, 6]])"
      ]
     },
     "execution_count": 229,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr4[ [True,False,False,True,False,True,True,False,False,False] ]   "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "driven-question",
   "metadata": {},
   "source": [
    "### 形状的改变"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 231,
   "id": "conservative-people",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11])"
      ]
     },
     "execution_count": 231,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = np.arange(12)\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 232,
   "id": "adequate-wings",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(12,)"
      ]
     },
     "execution_count": 232,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 235,
   "id": "sticky-india",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3],\n",
       "       [ 4,  5,  6,  7],\n",
       "       [ 8,  9, 10, 11]])"
      ]
     },
     "execution_count": 235,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.reshape((3, 4))  # reshape一定注意元素个数匹配"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 236,
   "id": "southwest-lease",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[ 0,  1],\n",
       "        [ 2,  3]],\n",
       "\n",
       "       [[ 4,  5],\n",
       "        [ 6,  7]],\n",
       "\n",
       "       [[ 8,  9],\n",
       "        [10, 11]]])"
      ]
     },
     "execution_count": 236,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.reshape((3, 2, 2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 245,
   "id": "cooked-circuit",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[[ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11]]]])"
      ]
     },
     "execution_count": 245,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.reshape((1,1,1,12))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 251,
   "id": "visible-nursing",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2],\n",
       "       [3, 4],\n",
       "       [5, 6]])"
      ]
     },
     "execution_count": 251,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = np.array([[1,2],[3,4],[5,6]])\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 252,
   "id": "lined-minneapolis",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [4, 5, 6]])"
      ]
     },
     "execution_count": 252,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.reshape(2,3)   # reshape返回了新的形状的数组，原数组没有变化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 253,
   "id": "treated-specific",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2],\n",
       "       [3, 4],\n",
       "       [5, 6]])"
      ]
     },
     "execution_count": 253,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 254,
   "id": "suffering-corporation",
   "metadata": {},
   "outputs": [],
   "source": [
    "data.resize((2,3))    # resize没有返回值, 直接修改了原数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 255,
   "id": "similar-lincoln",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [4, 5, 6]])"
      ]
     },
     "execution_count": 255,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "mysterious-florist",
   "metadata": {},
   "source": [
    "**在numpy里面，如果没有返回值，则修改了原数据，有返回值，一般不修改原数据**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 258,
   "id": "intense-carol",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[ 0,  1],\n",
       "        [ 2,  3],\n",
       "        [ 4,  5]],\n",
       "\n",
       "       [[ 6,  7],\n",
       "        [ 8,  9],\n",
       "        [10, 11]]])"
      ]
     },
     "execution_count": 258,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = np.arange(12).reshape(2,3,2)\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 261,
   "id": "daily-whole",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11])"
      ]
     },
     "execution_count": 261,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data1 = data.flatten()   # 将原始数据拉平\n",
    "data1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 262,
   "id": "treated-accordance",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(12,)"
      ]
     },
     "execution_count": 262,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data1.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 266,
   "id": "continuous-meaning",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11])"
      ]
     },
     "execution_count": 266,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.reshape(12)   # 等价于flatten"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 269,
   "id": "double-internship",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0, -1,  1, ...,  5,  4, -4],\n",
       "       [ 3,  8,  2, ..., -1,  2,  1],\n",
       "       [-1, -1, -9, ...,  0,  4,  0],\n",
       "       ...,\n",
       "       [-7,  5, -2, ...,  8, -3, 11],\n",
       "       [ 1,  2,  3, ...,  3,  2, -3],\n",
       "       [-2,  4, -3, ...,  6, -2, -1]])"
      ]
     },
     "execution_count": 269,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stock_day_rise.astype(np.int)  # 修改原始类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 271,
   "id": "organic-candy",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.95, -1.08,  1.73, ...,  5.33,  4.19, -4.86],\n",
       "       [ 3.21,  8.46,  2.56, ..., -1.52,  2.26,  1.91],\n",
       "       [-1.14, -1.62, -9.32, ...,  0.53,  4.08,  0.36],\n",
       "       ...,\n",
       "       [-7.87,  5.34, -2.61, ...,  8.15, -3.79, 11.98],\n",
       "       [ 1.42,  2.55,  3.48, ...,  3.05,  2.87, -3.82],\n",
       "       [-2.1 ,  4.26, -3.3 , ...,  6.14, -2.15, -1.88]])"
      ]
     },
     "execution_count": 271,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.round(stock_day_rise,2)       # 保存小数点后几位"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 273,
   "id": "unknown-assault",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.954, -1.084,  1.725, ...,  5.334,  4.192, -4.859],\n",
       "       [ 3.212,  8.46 ,  2.56 , ..., -1.522,  2.262,  1.907],\n",
       "       [-1.143, -1.621, -9.322, ...,  0.533,  4.085,  0.36 ],\n",
       "       ...,\n",
       "       [-7.865,  5.338, -2.605, ...,  8.145, -3.794, 11.982],\n",
       "       [ 1.419,  2.548,  3.478, ...,  3.055,  2.875, -3.823],\n",
       "       [-2.104,  4.258, -3.298, ...,  6.139, -2.151, -1.884]])"
      ]
     },
     "execution_count": 273,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stock_day_rise.round(3)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "surprising-audio",
   "metadata": {},
   "source": [
    "### 数组转置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 274,
   "id": "endless-direction",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2],\n",
       "       [3, 4],\n",
       "       [5, 6]])"
      ]
     },
     "execution_count": 274,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = np.array([[1,2],[3,4],[5,6]])\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 275,
   "id": "three-allah",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 3, 5],\n",
       "       [2, 4, 6]])"
      ]
     },
     "execution_count": 275,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 277,
   "id": "governing-investment",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 2],\n",
       "       [3, 4, 5],\n",
       "       [6, 7, 8]])"
      ]
     },
     "execution_count": 277,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data1 = np.arange(9).reshape(3,3)\n",
    "data1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 278,
   "id": "mental-today",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 3, 6],\n",
       "       [1, 4, 7],\n",
       "       [2, 5, 8]])"
      ]
     },
     "execution_count": 278,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data1.T   # 沿着主轴  翻转，行变成列，列变成行"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "municipal-monster",
   "metadata": {},
   "source": [
    "### 逻辑运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "threatened-angel",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[5, 6, 8, 3],\n",
       "       [0, 8, 9, 0],\n",
       "       [2, 8, 1, 4]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "temp = np.random.randint(0, 10, size=(3,4))\n",
    "temp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "equivalent-resort",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[False,  True,  True, False],\n",
       "       [False,  True,  True, False],\n",
       "       [False,  True, False, False]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "temp > 5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "junior-miami",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([6, 8, 8, 9, 8])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "temp[temp > 5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "welsh-tower",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.all([True, True, True])     # 相当于对数组里面所有数据求与运算， 如果所有数据为True那么就是True， 只要有一个为False最终结果就是False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "interesting-transmission",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3, 4, 5, 6, 8, 9])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.unique(temp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "animated-lambda",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 1, 0],\n",
       "       [0, 1, 1, 0],\n",
       "       [0, 1, 0, 0]])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.where(temp > 5, 1, 0) "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "miniature-christianity",
   "metadata": {},
   "source": [
    "### 统计运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "identified-singing",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[5, 6, 8, 3],\n",
       "       [0, 8, 9, 0],\n",
       "       [2, 8, 1, 4]])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "temp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "later-riding",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "9"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.max(temp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "intellectual-action",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.argmax(temp)   # 返回的是最大值的下标"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "adult-campaign",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([5, 8, 9, 4])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.max(temp, axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "surprised-integral",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 1, 2], dtype=int64)"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.argmax(temp, axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "nominated-journalist",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([8, 9, 8])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.max(temp, axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "previous-driving",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.min(temp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "sophisticated-outdoors",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.argmin(temp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "honey-kidney",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([3, 0, 1])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.min(temp, axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "prospective-continuity",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 6, 1, 0])"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.min(temp, axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "controversial-transport",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5.188029659257845"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.mean(temp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "british-quick",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 5.03722267,  1.04453851,  5.02896188,  7.33701469,  6.29523207,\n",
       "         8.80113664,  6.89422192,  7.79516527,  8.98996494,  1.26490088],\n",
       "       [ 0.75560875,  4.83885413, -0.59904152,  6.1710133 ,  8.51277783,\n",
       "         2.03235475,  8.88916522,  7.14942476,  3.27761971,  6.28098921],\n",
       "       [-0.73129417,  7.2948101 ,  4.06638121,  3.10274806,  4.52575061,\n",
       "         2.99431092,  1.10699327,  1.53194203, -0.36788439,  1.65474911],\n",
       "       [ 2.95295886,  2.21843497,  6.72551012,  4.63099207,  7.54453572,\n",
       "         3.9486903 ,  8.46140154,  2.97961213, -1.39971655, -2.43581363],\n",
       "       [ 6.14584823,  4.00204376,  5.53170619, 10.10313782,  5.8222812 ,\n",
       "         3.81343468,  0.80310628,  7.04612617,  3.4031976 ,  2.19416515],\n",
       "       [ 9.03821317,  3.65586345,  6.42085552, -0.49876038,  8.38224656,\n",
       "         7.78696999,  2.50294722,  8.99842764,  6.39529589,  7.29613701],\n",
       "       [ 8.07061021,  6.65934757,  2.74202237, -0.34884464,  8.58759905,\n",
       "         4.41654783, 12.93161747,  4.86906463,  5.20605048,  4.36572337],\n",
       "       [11.47721812, 10.94979136,  6.51980919, 13.03363641,  4.38151045,\n",
       "         4.21878292, -0.64097244,  5.24572922,  1.75354507,  5.71889324],\n",
       "       [ 5.65763577, 10.23241051,  1.40473993,  5.84568903,  3.63388766,\n",
       "         5.25422885,  5.18682212,  6.4616095 ,  4.4673827 , -2.36292787],\n",
       "       [ 2.83801241,  4.12696666,  1.94716548,  5.1756292 ,  3.75867721,\n",
       "         0.67092948,  2.80413651,  8.17714618,  8.65036731,  4.22035277]])"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "temp = np.random.normal(5, 3, size=(10,10))\n",
    "temp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "fewer-three",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3.2521012980754853"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.std(temp)   # 求数组的方差"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "noticed-resistance",
   "metadata": {},
   "source": [
    "### 数组与数的运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "competent-stack",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 2],\n",
       "       [3, 4, 5]])"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = np.arange(6).reshape((2,3))\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "chinese-university",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[6, 5, 4],\n",
       "       [3, 2, 1]])"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data1 = np.arange(6,0,-1).reshape((2,3))\n",
    "data1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "painted-rendering",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  2,  4],\n",
       "       [ 6,  8, 10]])"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data * 2   # + - * / 会扩展到每一个元素上面去"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "durable-charles",
   "metadata": {},
   "source": [
    "### 数组间运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "mediterranean-conditions",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[6, 6, 6],\n",
       "       [6, 6, 6]])"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data + data1     # 数组间的 + - * / 也是 对应位置相加"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "fancy-wholesale",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 5, 8],\n",
       "       [9, 8, 5]])"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data * data1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "supposed-field",
   "metadata": {},
   "source": [
    "### 矩阵乘法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "touched-duncan",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 2],\n",
       "       [3, 4, 5]])"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "continuous-click",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[6, 5],\n",
       "       [4, 3],\n",
       "       [2, 1]])"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data1.resize((3,2))\n",
    "data1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "insured-marketplace",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 8,  5],\n",
       "       [44, 32]])"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.matmul(data, data1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "finnish-framework",
   "metadata": {},
   "source": [
    "![](imgs/Snipaste_2021-10-08_15-05-29.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "smaller-subsection",
   "metadata": {},
   "source": [
    "* 两个矩阵相乘中间的边要相等\n",
    "* 最终的结果数组的形状 是取第一个数组的行和第二个数组的列\n",
    "* 结果数组的第(n,m)位置的数 是第一个数组的第n行按位置乘以第二个数组的m列相加的结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "equivalent-latex",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[15, 26, 37],\n",
       "       [ 9, 16, 23],\n",
       "       [ 3,  6,  9]])"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.matmul(data1, data)    # 矩阵乘法  谁写前面和后面 结果是完全不同的"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "welsh-lunch",
   "metadata": {},
   "source": [
    "### 广播机制"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "private-scoop",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 2],\n",
       "       [3, 4, 5]])"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data   # shape 2x3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "destroyed-liver",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3])"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data1 = np.array([1, 2, 3])\n",
    "data1    # shape (3,)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "substantial-blues",
   "metadata": {},
   "outputs": [],
   "source": [
    "# data1 = np.array([[1, 2, 3],\n",
    "#                   [1, 2, 3]])   # 相当于将data1 扩展成一个 2x3的数组相加， 往边为1的方向扩展"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "limiting-symbol",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 3, 5],\n",
       "       [4, 6, 8]])"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data + data1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "appointed-animation",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1],\n",
       "       [2],\n",
       "       [3]])"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data1.resize(3,1)\n",
    "data1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "swiss-ocean",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 2],\n",
       "       [3, 4, 5],\n",
       "       [6, 7, 8]])"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = np.arange(9).reshape((3,3))\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "outside-homework",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1,  2,  3],\n",
       "       [ 5,  6,  7],\n",
       "       [ 9, 10, 11]])"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data + data1    # 如果要想能够相加必须有一个边对齐，另外一个边是1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "accompanied-malawi",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 2],\n",
       "       [3, 4, 5],\n",
       "       [6, 7, 8]])"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "legitimate-franklin",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2])"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data1 = np.array([2])\n",
    "data1    #  [2, 2, 2]\n",
    "         #  [2, 2, 2]\n",
    "         #  [2, 2, 2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "continued-publisher",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 2,  3,  4],\n",
       "       [ 5,  6,  7],\n",
       "       [ 8,  9, 10]])"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data + data1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "joined-indonesia",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 2],\n",
       "       [3, 4, 5],\n",
       "       [6, 7, 8]])"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "presidential-roller",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2])"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data1 = np.array([1, 2])\n",
    "data1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "electoral-uncertainty",
   "metadata": {},
   "source": [
    "* 至少有一个边为1\n",
    "* 另外一个边要么为1 要么对齐"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "national-piece",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = np.array([[80,86],\n",
    "[82,80],\n",
    "[85,78],\n",
    "[90,90],\n",
    "[86,82],\n",
    "[82,90],\n",
    "[78,80],\n",
    "[92,94]])\n",
    "\n",
    "b = np.array([[0.7], \n",
    "              [0.3]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "expressed-latin",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(8, 2)"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "postal-silence",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2, 1)"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "fiscal-desire",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[81.8],\n",
       "       [81.4],\n",
       "       [82.9],\n",
       "       [90. ],\n",
       "       [84.8],\n",
       "       [84.4],\n",
       "       [78.6],\n",
       "       [92.6]])"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.matmul(a, b)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "medieval-status",
   "metadata": {},
   "source": [
    "### 合并与分割"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "id": "lasting-repeat",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 2],\n",
       "       [3, 4, 5],\n",
       "       [6, 7, 8]])"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "secondary-frame",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 1, 1],\n",
       "       [1, 1, 1],\n",
       "       [1, 1, 1]])"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ones = np.ones((3,3), dtype=np.int)\n",
    "ones"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "id": "little-strike",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 2],\n",
       "       [3, 4, 5],\n",
       "       [6, 7, 8],\n",
       "       [1, 1, 1],\n",
       "       [1, 1, 1],\n",
       "       [1, 1, 1]])"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.concatenate((data, ones))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "id": "encouraging-worse",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 2, 1, 1, 1],\n",
       "       [3, 4, 5, 1, 1, 1],\n",
       "       [6, 7, 8, 1, 1, 1]])"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.concatenate((data, ones), axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "id": "minimal-milwaukee",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 2, 1, 1, 1],\n",
       "       [3, 4, 5, 1, 1, 1],\n",
       "       [6, 7, 8, 1, 1, 1]])"
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1 = np.hstack((data, ones))\n",
    "arr1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "id": "preceding-robin",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 2],\n",
       "       [3, 4, 5],\n",
       "       [6, 7, 8],\n",
       "       [1, 1, 1],\n",
       "       [1, 1, 1],\n",
       "       [1, 1, 1]])"
      ]
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.vstack((data, ones))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "id": "stylish-genesis",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[0, 1, 2, 1, 1, 1]]),\n",
       " array([[3, 4, 5, 1, 1, 1]]),\n",
       " array([[6, 7, 8, 1, 1, 1]])]"
      ]
     },
     "execution_count": 81,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.split(arr1, 3, axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "id": "limiting-velvet",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 2, 1, 1, 1],\n",
       "       [3, 4, 5, 1, 1, 1],\n",
       "       [6, 7, 8, 1, 1, 1]])"
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "id": "mighty-needle",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[0, 1, 2, 1, 1, 1]]),\n",
       " array([[3, 4, 5, 1, 1, 1]]),\n",
       " array([[6, 7, 8, 1, 1, 1]])]"
      ]
     },
     "execution_count": 91,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.split(arr1, 3)   # split一定要能够整除份数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "circular-asset",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "placed-pregnancy",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
